package com.lepu.app.fun.chat.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;

import com.eyzhs.app.R;
import com.lepu.app.fun.chat.business.uiflow.UITransition;
import com.lepu.app.fun.chat.controller.base.AbsBaseFragment;
import com.lepu.app.fun.chat.controller.base.BaseFragmentActivityChat;

import common.db.Constants;


/**
 * Created by chuanyhu on 2014/12/23.
 */
public class CommonActivity extends BaseFragmentActivityChat {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        try {
            mNeedSwipeBack = false;
            
            super.onCreate(savedInstanceState);
            setContentView(R.layout.app_activity_common);
            Intent intent = this.getIntent();
            Bundle bundleParam = intent.getExtras();
            String fragmentClsName = bundleParam.getString(Constants.PARAM_TARGET_FRAGMENT);
            Bundle bundle = bundleParam.getBundle(Constants.PARAM_BUNDLE_PARAM);
            AbsBaseFragment targetFragment = (AbsBaseFragment) Class.forName(fragmentClsName).newInstance();
            targetFragment.setArguments(bundle);
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.replace(R.id.layout_common_holder, targetFragment).commit();
            UITransition.setFragTransRightInRightOut(transaction);

        } catch (Exception e) {
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        AbsBaseFragment currentFragtment = getCurrentFragment();
        boolean fragmentResult = currentFragtment != null ? currentFragtment.onKeyDown(keyCode, event) : false;
        if (fragmentResult) {
            return fragmentResult;
        } else if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_DOWN &&
                getSupportFragmentManager().getBackStackEntryCount() == 0) {
            finish();
            UITransition.setActivityFinishRightOut(this);
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }
}
